package com.yimi.yinhepda.views.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.pda.uce.commom.util.LoginUserModule;
import com.pda.ym.utils.SharedPreferUtil;
import com.uce.pda.util.MediaPlayerUtil;
import com.yimi.yinhepda.MainActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.dao.FuncBtnTableDao;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.views.main.function_module.NewFunctionFragment;
import com.yimi.yinhepda.views.main.home_module.HomeFragment;
import com.yimi.yinhepda.views.main.my_module.MyFragment;
import com.yimi.yinhepda.views.main.util.UcMainUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/9/17 11:25
 * Description: 主页
 */
public class NewMainActivity extends MainActivity {

    private ViewPager viewPager;
    private MenuItem menuItem;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragments;
    private VpAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_main);
        MediaPlayerUtil.setValue(BaseApplication.getContext(), 100);
        viewPager = findViewById(R.id.viewpager);
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        LoginUserModule.getInstance().insertLoginUser();

        //初始化功能按钮
        initFunctionBtn();

        bottomNavigationView.getMenu().getItem(0).setIcon(R.mipmap.zhuye_nb);
        //默认 >3 的选中效果会影响ViewPager的滑动切换时的效果，故利用反射去掉
//        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Common.specificSetting(NewMainActivity.this);
                        switch (item.getItemId()) {
                            case R.id.item_shouye:
                                //选中时加载选中的图片
                                item.setIcon(R.mipmap.zhuye_nb);
                                viewPager.setCurrentItem(0);
                                break;
                            case R.id.item_gongneng:
                                item.setIcon(R.mipmap.gongneng_nb);
                                viewPager.setCurrentItem(1);
                                break;
                            case R.id.item_my:
                                item.setIcon(R.mipmap.my_nb);
                                viewPager.setCurrentItem(2);
                                break;
                            default:
                                break;
                        }
                        return false;
                    }
                });
        viewPager.setOffscreenPageLimit(2);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        fragments = new ArrayList<>(3);
        fragments.add(new HomeFragment());
//      fragments.add(new FunctionFragment());
        fragments.add(new NewFunctionFragment());
        fragments.add(new MyFragment());
        adapter = new VpAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);

        new UcMainUtil().ucCreate();
    }

    /**
     * view pager adapter
     */
    private static class VpAdapter extends FragmentPagerAdapter {
        private List<Fragment> data;

        public VpAdapter(FragmentManager fm, List<Fragment> data) {
            super(fm);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }
    }

    /**
     * 初始化功能按钮
     */
    private void initFunctionBtn() {
        List<FuncBtnTable> btnTableList = new ArrayList<>();
        int deptType = SharedPreferUtil.getInstance().getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            btnTableList.add(new FuncBtnTable("叉车分拣", Common.FUNC_CCFJ, 0, 2, Common.FUNC_GROUP_INSIDE_CODE, 1, 0, 0));
        } else {
            btnTableList.add(new FuncBtnTable("叉车分拣", Common.FUNC_CCFJ, 0, 2, Common.FUNC_GROUP_INSIDE_CODE, 0, 0, 0));
        }

        btnTableList.add(new FuncBtnTable("回流件", Common.FUNC_HLJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 1, 1));
        btnTableList.add(new FuncBtnTable("留仓发件", Common.FUNC_LCFJ, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 1, 1));
        btnTableList.add(new FuncBtnTable("小件扫描", Common.FUNC_SJIANSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));
        btnTableList.add(new FuncBtnTable("小信封扫描", Common.FUNC_XXFSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));
        btnTableList.add(new FuncBtnTable("收件扫描", Common.FUNC_SJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 1));
        btnTableList.add(new FuncBtnTable("收发扫描", Common.FUNC_SFSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));
        btnTableList.add(new FuncBtnTable("派件扫描", Common.FUNC_PJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));
        btnTableList.add(new FuncBtnTable("到派扫描", Common.FUNC_DPSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));

        //快运功能
        btnTableList.add(new FuncBtnTable("创建盘点", Common.FUNC_CJPD, 0, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("复称检查", Common.FUNC_FCJC, 0, 1, Common.FUNC_GROUP_EXCEPTION_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("配送任务", Common.FUNC_PSRW, 0, 3, Common.FUNC_GROUP_OPTER_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("盘点列表", Common.FUNC_PDLB, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("盘点差异", Common.FUNC_PDCY, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("问题件上报", Common.FUNC_WENTJSB, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("无头件上报", Common.FUNC_WUTJSB, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("配送退单", Common.FUNC_PSTD, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("单独绑定", Common.FUNC_DDBD, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("运单入库", Common.FUNC_YDRK, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable("场内分拣", Common.FUNC_CNFJ, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 0, 0));
//        btnTableList.add(new FuncBtnTable("(旧)卸车扫描", Common.FUNC_JXCSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("揽货扫描", Common.FUNC_LHSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 0));
//        btnTableList.add(new FuncBtnTable("异常管理", Common.FUNC_YCGL, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 0, 0, 0));
//        btnTableList.add(new FuncBtnTable("建包管理", Common.FUNC_JBGL, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 0));
//        btnTableList.add(new FuncBtnTable("建包操作", Common.FUNC_JBCZ, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 0));
//        btnTableList.add(new FuncBtnTable("解包操作", Common.FUNC_JBCZV, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("移库倒货", Common.FUNC_YKDH, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("签到管理", Common.FUNC_QDGL, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 0, 0, 0));
        btnTableList.add(new FuncBtnTable("分拨自提", Common.FUNC_FBZT, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 0, 0));
        btnTableList.add(new FuncBtnTable(getString(R.string.kuayue_unload), Common.FUNC_KYDH, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 0, 0));
        //快递功能
        btnTableList.add(new FuncBtnTable("仓位上报", Common.FUNC_CWSB, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 1, 0));
        btnTableList.add(new FuncBtnTable("安检扫描", Common.FUNC_AJSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 1, 1));
        btnTableList.add(new FuncBtnTable("差错上报", Common.FUNC_CCSB, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 0, 1));

        btnTableList.add(new FuncBtnTable("入仓扫描", Common.FUNC_RCSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 0, 1));
        btnTableList.add(new FuncBtnTable("实名验收", Common.FUNC_SMZYS, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 0, 1));
        btnTableList.add(new FuncBtnTable("建包扫描", Common.FUNC_JBSM, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 2, 1));

        btnTableList.add(new FuncBtnTable("领件扫描", Common.FUNC_LJSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 2, 1));
        btnTableList.add(new FuncBtnTable("留仓扫描", Common.FUNC_LCSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("回单到件", Common.FUNC_HDDJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 2, 1));

        btnTableList.add(new FuncBtnTable("回单发件", Common.FUNC_HDFJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("网点交接", Common.FUNC_WDJJSM, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));


        btnTableList.add(new FuncBtnTable("找货登记", Common.FUNC_ZHDJSM, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("一键到达", Common.FUNC_YJDD, 0, Common.FUNC_GROUP_OPTER_CODE, 0, 2, 1));

        btnTableList.add(new FuncBtnTable("解包扫描", Common.FUNC_JBASM, 0, Common.FUNC_GROUP_OPTER_CODE, 1, 2, 1));

        btnTableList.add(new FuncBtnTable("一键留仓", Common.FUNC_YJLC, 0, Common.FUNC_GROUP_INSIDE_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("退件扫描", Common.FUNC_TJSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 2, 1));

        btnTableList.add(new FuncBtnTable("差错查询", Common.FUNC_CCCX, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("快件跟踪", Common.FUNC_KJGZ, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 1, 2, 1));
        btnTableList.add(new FuncBtnTable("复称扫描", Common.FUNC_FCSM, 0, Common.FUNC_GROUP_INSIDE_CODE, 0, 2, 1));

        btnTableList.add(new FuncBtnTable("上传统计", Common.FUNC_SCTJ, 0, Common.FUNC_GROUP_EXCEPTION_CODE, 0, 2, 1));

        boolean isInitFunc = sharedPreferUtil.getBooleanValue(Common.IS_INIT_FUNC);
        FuncBtnTableDao funcTableDao = new FuncBtnTableDao();
        List<FuncBtnTable> allData = funcTableDao.findAll();
        if (!isInitFunc || allData == null || allData.isEmpty()) {
            sharedPreferUtil.setBooleanValue(Common.IS_INIT_FUNC, true);
            funcTableDao.deleteAll();
            funcTableDao.createOrUpdate(btnTableList);
        }
    }
}
